﻿
@{
    ViewBag.Title = "Expense_audit";
}

<h2>运费审核</h2>

<div>
    <fieldset class="boun nav" style="padding-bottom:6px;">
        <legend class="len"></legend>
        <div class="col-lg-3">承运单位:</div>
        <div class="col-lg-3" id="CYDW"></div>
    </fieldset>
</div>
<div style="display:none" id="GS_id">@(Session["gs_id"])</div>
<div id="sh_klj"></div>
<div>
    <button id="tg">通过</button>
    <button id="bohui">驳回</button>
    <button id="back_1">返回</button>
</div>

@section scripts
{
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>
    <script>
        $(function () {
            get_sh_xx()
            //返回列表
            $(document).on("click", "#back_1", function () {
                window.location.href = "/Freight/Carrier_audit";
            })
            //不通过
            $(document).on("click", "#bohui", function () {
                $.ajax({
                    url: '/Freight/Expense_audit',
                    type: 'POSt',
                    data: { "state": 3, "id": $("#GS_id").text() },
                    success: function (data) {
                        if (data == "1") {
                            alert("审核结束，报价不通过")
                            window.location.href = "/Freight/Carrier_audit";
                        }
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            })
            //通过
            $(document).on("click", "#tg", function () {
                console.log($("#GS_id").text())
                $.ajax({
                    url: '/Freight/Expense_audit',
                    type: 'GET',
                    data: { "state": 4, "id": $("#GS_id").text()},
                    success: function (data) {
                        console.log(data)
                        if (data == "1") {
                            alert("审核结束,报价通过")
                            add_yf()
                            window.location.href = "/Freight/Carrier_audit";
                        }
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            })

            function add_yf() {
                var yuf = []
                $(".nijs tr").each(function (i, item) {
                    yuf.push({
                        jw_bian: $(".bh_sj", item).text(),
                        jw_jt: $(".JT_jtw",item).val(),
                        jw_jg: $(".JT_jtj", item).val(),
                        jw_cx: $(".cx_sj", item).text(),
                        jw_gs: $("#GS_id").text()
                    })
                })

                $.ajax({
                    url: "/Freight/ADD_SH_YF",
                    type: 'POST',
                    data: JSON.stringify(yuf),
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (text) {
                        alert("阶梯价提交审核")
                    },
                    error: function (text) {
                        alert("系统错误" + text);
                    }
                })
            }


            function get_sh_xx() {
                $.ajax({
                    url: '/Freight/Expense_audit',
                    type: 'POST',
                    data: { "state": 1, "id": $("#GS_id").text() },
                    success: function (data) {
                        createRow_VT(data)
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            }

            function createRow_VT(data) {
                $("#sh_klj table").remove()
                $.each(data, function (i, item) {
                    var temp=0
                    $(".chex").each(function (i, obj) {
                        if ($(obj).text() == item.c_name) {
                            temp++
                        }
                    })
                    
                    $("#CYDW").text(item.gs_name)
                    if (temp == 0) {
                        var $table =
                            $(`
                                <table>
                                    <thead>
                                        <tr>
                                            <th class="chex">${item.c_name}</th>
                                        </tr>
                                        <tr>
                                            <th>阶梯</th>
                                            <th></th>
                                            <th>价格</th>
                                            <th>单位</th>
                                        </tr>
                                    </thead>
                                    <tbody id="${item.ss_cx}" class="nijs">
                
                                    </tbody>
                                </table>
                            `)
                        $("#sh_klj").append($table)
                        $("#" + item.ss_cx + " tr").remove()
                        $.ajax({
                            url: '/Freight/Expense_audit',
                            type: 'GET',
                            data: { "state": 2, "id": item.ss_cx },
                            success: function (pa) {
                                $.each(pa, function (i, obj) {
                                    if (obj.ss_bian == "起步") {
                                        var $tr =
                                            $(`
        <tr>
            <td><input type="number" value="${obj.ss_jt}" style="width:60px" class="JT_jtw"/></td>
            <td style="width:60px">公里以下</td>
            <td><input type="number" value="${obj.ss_jg}" style="width:60px" class="JT_jtj"/></th>
            <td style="width:60px">元/车</th>
            <td style="display:none" class="cx_sj">${item.ss_cx}</td>
            <td style="display:none" class="bh_sj">${obj.ss_bian}</td>
        </tr>
    `)
                                    }
                                    else if (obj.ss_bian == "阶梯") {
                                        var $tr =
                                            $(`
        <tr>
            <td><input type="number" value="${obj.ss_jt}" style="width:60px" class="JT_jtw"/></td>
            <td style="width:60px">公里以下</td>
            <td><input type="number" value="${obj.ss_jg}" style="width:60px" class="JT_jtj"/></th>
            <td style="width:60px">元/公里</th>
            <td style="display:none" class="cx_sj">${item.ss_cx}</td>
            <td style="display:none" class="bh_sj">${obj.ss_bian}</td>
        </tr>
    `)
                                    }
                                    else {
                                        var $tr =
                                            $(`
        <tr>
            <td><input type="number" value="${obj.ss_jt}" style="width:60px" class="JT_jtw"/></td>
            <td style="width:60px">公里以上</td>
            <td><input type="number" value="${obj.ss_jg}" style="width:60px" class="JT_jtj"/></th>
            <td style="width:60px">元/公里</th>
            <td style="display:none" class="cx_sj">${item.ss_cx}</td>
            <td style="display:none" class="bh_sj">${obj.ss_bian}</td>
        </tr>
    `)
                                    }
                                    $("#" + item.ss_cx + "").append($tr)
                                })

                            },
                            error: function () {
                                alert('Request Error');
                            }
                        })
                    }
                    
                    
                })
                
            }
        })
    </script>
}